# $Id: debug.tcl 44 2007-09-03 00:47:25Z khomoutov $
# Debugging facilities -- stub hook handlers provided to just dump parsed data.
# Should be removed eventually.

namespace eval ::dcclient {
	hook add c2s_in_pub_msg   [mycmd ProcessChatMessage public] -iscommand -prio 100
	hook add c2s_in_cmd_lock  [mycmd ProcessCmdLock] -iscommand -prio 100
	hook add c2s_in_cmd_hello [mycmd ProcessCmdHello] -iscommand -prio 100
	hook add c2s_in_cmd_quit  [mycmd ProcessCmdQuit] -iscommand -prio 100
	hook add c2s_in_cmd_myinfo    [mycmd ProcessCmdMyInfo] -iscommand -prio 100
	hook add c2s_in_cmd_search    [mycmd ProcessCmdSearch] -iscommand -prio 100
	hook add c2s_in_cmd_forcemove [mycmd ProcessCmdForceMove] -iscommand -prio 100
	hook add c2s_in_cmd_hubname   [mycmd ProcessCmdHubName] -iscommand -prio 100
	hook add c2s_in_cmd_connecttome [mycmd ProcessCmdConnectToMe] -iscommand -prio 100
	hook add c2s_in_cmd_revconnecttome [mycmd ProcessCmdRevConnectToMe] -iscommand -prio 100
	hook add c2s_in_unknown_cmd   [mycmd ProcessUnknownCmd] -iscommand -prio 100
}

proc ::dcclient::ProcessChatMessage {type hub level vnick vmsg} {
	upvar $level $vnick nick $vmsg msg

	debugmsg inchat "$hub: MSG ($type) FROM: $nick TEXT: $msg"
}

proc ::dcclient::ProcessCmdLock {hub level vlock vpk} {
	upvar $level $vlock lock $vpk pk

	debugmsg incmd "$hub: LOCK: $lock; PK: $pk"
}

proc ::dcclient::ProcessCmdHello {hub level vnick} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vnick nick

	set state(users) [lsort -unique [lappend state(users) $nick]]

	debugmsg incmd "$hub: JOINED: $nick| [llength $state(users)] users"
}

proc ::dcclient::ProcessCmdQuit {hub level vnick} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vnick nick

	set ix [lsearch -sorted $state(users) $nick]
	if {$ix >= 0} {
		set state(users) [lreplace $state(users) $ix $ix]
	}

	debugmsg incmd "$hub: QUIT: $nick| [llength $state(users)] users"
}

proc ::dcclient::ProcessCmdMyInfo {hub level
		vnick vinterest vspeed vspeedclass vemail vsharesize} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vnick nick $vinterest interest $vspeed speed \
		$vspeedclass speedclass $vemail email $vsharesize sharesize

	debugmsg incmd "$hub: \$MyINFO: nick: $nick; interest: $interest;\
		speed: $speed; speed class: $speedclass;\
		email: $email; sharesize: $sharesize"
}

proc ::dcclient::ProcessCmdSearch {hub level
		vwho vsizerestricted visminsize vsize vtype vkeywords} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vwho who \
		$vsizerestricted sizerestricted $visminsize isminsize $vsize size \
		$vtype type $vkeywords keywords

	debugmsg incmd "$hub: \$Search: $who; rest? $sizerestricted;\
		minsize? $isminsize; size: $size; $type; <$keywords>"
}

proc ::dcclient::ProcessCmdForceMove {hub level vdest} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vdest dest

	debugmsg imcmd "$hub: \$ForceMove to $dest"
	debugmsg failure "$hub: forced movement to $dest"
}

proc ::dcclient::ProcessCmdHubName {hub level vname} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vname name

	set state(hubname) $name

	debugmsg imcmd "$hub: \$HubName: $name"
}

proc ::dcclient::ProcessCmdConnectToMe {hub level vmynick vhost vport} {
	variable $hub
	upvar 0 $hub state
	upvar $level $vmynick mynick $vhost host $vport port

	debugmsg incmd "$hub: \$ConnectToMe: To: $mynick| From: <$host>:<$port>|"

	debugmsg peer "$hub: \$ConnectToMe: To: $mynick| From: <$host>:<$port>|"
	if 0 {
	after 0 [mycmd \
		c2c::c2c_connect -host $host -port $port -mynick $mynick \
			-encoding [fconfigure $state(sock) -encoding]]
	}
	# TODO get encoding from the hub's state
}

proc ::dcclient::ProcessCmdRevConnectToMe {hub level vmynick vsender} {
	upvar $level $vmynick mynick $vsender sender

	debugmsg incmd "$hub: \$RevConnectToMe: To: $mynick| From: $sender|"
}

proc ::dcclient::ProcessUnknownCmd {hub level vcmd vparam} {
	upvar $level $vcmd cmd $vparam param

	debugmsg incmd "$hub: UNKNOWN: cmd: $cmd; param: $param"
}

